#include "talkToFather.h"

namespace lvpublisher
{

TalkToFather & TalkToFather::getInstance()
{
    static TalkToFather instance;
    return instance;
}

void TalkToFather::init(uint16_t port, const std::string & ip)
{
    m_fd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&m_addr, 0, sizeof(m_addr));
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons(port);
    m_addr.sin_addr.s_addr = inet_addr(ip.c_str());
}

void TalkToFather::wantStop(const std::string & SN_ID)
{
    std::string sendMsg;
    sendMsg += "GET /Ros/disConnect?SN_ID="+ SN_ID +" HTTP/1.1\r\n\r\n";
    connect(m_fd, (const struct sockaddr*)&m_addr, sizeof(m_addr));
    send(m_fd, sendMsg.c_str(), sendMsg.size(), 0);
    char buf[1024];
    recv(m_fd, buf, 1024, 0);
    shutdown(m_fd, SHUT_RDWR);
}

void TalkToFather::changeState(const std::string & state)
{
    std::string sendMsg;
    sendMsg += "GET /Ros/changeState?state="+ state +" HTTP/1.1\r\n\r\n";
    connect(m_fd, (const struct sockaddr*)&m_addr, sizeof(m_addr));
    send(m_fd, sendMsg.c_str(), sendMsg.size(), 0);
    char buf[1024];
    recv(m_fd, buf, 1024, 0);
    shutdown(m_fd, SHUT_RDWR);
}

TalkToFather::TalkToFather()
{

}

TalkToFather::~TalkToFather()
{

}

}


